const obj = {
  name: "merci"
}
function getName(age, gender) {
  console.log(this.name, age, gender);
}

Function.prototype.mycall = function(obj, ...rest) {
  obj.fn = this
  let result = obj.fn(...rest)
  delete obj.fn
  return result
}

// getName.mycall(obj, "18", "male")

Function.prototype.myapply = function(obj, arr) {
  obj.fn = this
  let result = obj.fn(...arr)
  delete obj.fn
  return result
}

// getName.myapply(obj, ["18", "male"])


Function.prototype.mybind = function(obj, ...rest) {
  let _this = this
  let fbound = function (...rest2) {
    return _this.apply(this instanceof _this ? this : obj, [...rest, ...rest2])
  }
  let tempFn = function () {}
  tempFn.prototype = this.prototype
  fbound.prototype = new tempFn()
  return fbound
}

// getName.mybind(obj, "18")("male")


let obj2 = {
  value: "obj2的value"
}
function bar(name, age) {
  console.log(this.value);
  console.log(this);
  console.log(name);
  console.log(age);
}

// bar.bind(obj2, "merci", 18)()

const bindFn = bar.bind(obj2, "merci", 18)
const b = new bindFn()

console.log("----------------------------mybind------------------------------");

const bindFn2 = bar.mybind(obj2, "merci", 18)
const b2 = new bindFn2()